/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.common.enums.OrderType;
import com.lczyfz.campusinfo.common.enums.PayType;
import com.lczyfz.campusinfo.common.utils.ApiResultUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoAbroad;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoCountry;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoRegion;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoAbroadService;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoCountryService;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoRegionService;
import com.lczyfz.campusinfo.modules.campusinfoorder.service.CampusinfoOrderService;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import com.lczyfz.campusinfo.modules.sys.security.SystemAuthorizingRealm;
import com.lczyfz.campusinfo.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 国家管理Controller
 *
 * @author yt
 * @version 2017-10-20
 */
@Controller
@RequestMapping(value = "api/campusinfoeducation/campusinfoCountry")
public class CampusinfoCountryApiController extends BaseController {
    @Autowired
    private CampusinfoRegionService campusinfoRegionService;
    @Autowired
    private CampusinfoCountryService campusinfoCountryService;
    @Autowired
    private CampusinfoAbroadService campusinfoAbroadService;
    @Autowired
    private CampusinfoOrderService campusinfoOrderService;
    @Autowired
    private CampusinfoPayService campusinfoPayService;
    @RequestMapping(value = "countryList") //国家接口
    @ResponseBody
    public Map<String, Object> getcampusinfoCountry(CampusinfoCountry campusinfoCountry, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        List<CampusinfoCountry> campusinfoCountries = campusinfoCountryService.findList(campusinfoCountry);
        map.put("page",campusinfoCountries);
        return map;
    }
    @RequestMapping(value = "regionList")
    @ResponseBody  //地区接口
    public Map<String, Object> getcampusinfoRegion(CampusinfoRegion campusinfoRegion, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        List<CampusinfoRegion> campusinfoCountries = campusinfoRegionService.findList(campusinfoRegion);
        map.put("page",campusinfoCountries);
        return map;
    }
    @RequestMapping(value = "addAbroad") //线下支付
    @ResponseBody
    public Map<String, Object> addAbroad(CampusinfoAbroad campusinfoAbroad) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        campusinfoAbroad.setUserId(principal.toString());
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        if (campusinfoAbroad != null) {
            List<CampusinfoAbroad> campusinfoAbroads=campusinfoAbroadService.findList(campusinfoAbroad);
            if(campusinfoAbroads.isEmpty()){
                campusinfoAbroad.setOrderCode(campusinfoOrderService.saveOrderCode(OrderType.Abroad.getValue()));
                campusinfoAbroadService.save(campusinfoAbroad);
                CampusinfoCountry campusinfoCountry =  campusinfoCountryService.get(campusinfoAbroad.getCountryid());
                String Goodname =campusinfoCountry.getCountry()+OrderType.Abroad.getDesc();
                campusinfoPayService.ordersaveinsert(campusinfoAbroad.getOrderCode(),campusinfoAbroad.getPaymentMethod(),Goodname,campusinfoCountry.getPayment());
                map.put("message", "报名成功");
                map.put("campusinfoAbroad", campusinfoAbroad);
            } else {
                campusinfoAbroads.get(0).setPaymentMethod(PayType.UNline.getValue());
                campusinfoAbroadService.save( campusinfoAbroads.get(0));
                map.put("message", "用户已经报名");
            }
        } else {
            map.put("message", "报名失败");
        }
        return map;
    }
    @RequestMapping(value = "alipayAddAbroad")  //支付宝支付
    @ResponseBody
    public Map<String, Object> AlipayaddAbroad(CampusinfoAbroad campusinfoAbroad) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        campusinfoAbroad.setUserId(principal.toString());
        Map<String, Object> map = ApiResultUtils.getBaseMapSuccess();
        if (campusinfoAbroad != null) {
            CampusinfoAbroad findabroad = campusinfoAbroadService.getAlipay(campusinfoAbroad);
            if(findabroad==null){         //第一次报名
                campusinfoAbroad.setOrderCode(campusinfoOrderService.saveOrderCode(OrderType.Abroad.getValue()));
                campusinfoAbroadService.save(campusinfoAbroad);
                CampusinfoCountry campusinfoCountry =  campusinfoCountryService.get(campusinfoAbroad.getCountryid());
                String Goodname =campusinfoCountry.getCountry()+OrderType.Abroad.getDesc();
                campusinfoPayService.ordersaveinsert(campusinfoAbroad.getOrderCode(),campusinfoAbroad.getPaymentMethod(),Goodname,campusinfoCountry.getPayment());
                map.put("message", "报名成功");
                map.put("campusinfoAbroad", campusinfoAbroad);
            }
            else if(findabroad.getStatus().equals(CampusinfoOrderStat.UNDONE.getValue())) {  //判断是否支付状态完成
                map.put("message", "报名成功");
                map.put("campusinfoAbroad",findabroad);
            } else {
                map.put("success",false);
                map.put("message", "用户已经支付完成");
            }
        } else {
            map.put("message", "支付失败");
        }
        return map;
    }
}